<?php

namespace app\system\service;

use think\facade\Db;

class DepartmentUserService
{
    public function addDepartmentUser($data)
    {
        $userId = $data['id'];
        $saveData['userId'] = $data['id'];
        $saveData['id'] = $data['parentId'];
        //查询是否存在
        $departmentUserList = Db::table('department_user')->where('userId',$userId)->findOrEmpty();

        //查询出所有的部门名称
        $departmentList=Db::table('department')->select()->column('name', 'id');
        $saveData['name'] = $departmentList[$saveData['id']];
        if (empty($departmentUserList)) {
            $saveData['createdAt'] = date("Y-m-d H:i:s");
            //插入
            Db::name('department_user')->insert($saveData);
        } else {
            $saveData['updatedAt'] = date("Y-m-d H:i:s");
            //更新
            Db::name('department_user')->update($saveData);
        }
    }
}